
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Pages;


public class GenericPagesDAO extends AutoKeys implements org.jetnuke.dao.PagesDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#retrieveByPK(int)
	 */
	public Pages retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Pages  s = new Pages();
		
		if (rs.next()) {
			fillPagesFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillPagesFromResultSet(Pages s, ResultSet rs)throws Exception {
		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setCID(rs.getInt("CID"));
		s.setCLANGUAGE(rs.getString("CLANGUAGE"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setDATE(rs.getTimestamp("DATE"));
		s.setPAGE_FOOTER(rs.getString("PAGE_FOOTER"));
		s.setPAGE_HEADER(rs.getString("PAGE_HEADER"));
		s.setPID(rs.getInt("PID"));
		s.setSIGNATURE(rs.getString("SIGNATURE"));
		s.setSUBTITLE(rs.getString("SUBTITLE"));
		s.setTEXT(rs.getString("TEXT"));
		s.setTITLE(rs.getString("TITLE"));
		
	}

	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Pages u = new Pages();
			fillPagesFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.selectAll"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#update(org.jetnuke.entities.Pages)
	 */
	public void update(Pages pg) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.update"));
		p.setInt(1,pg.getACTIVE());
		p.setInt(2,pg.getCID());
		p.setString(3,pg.getCLANGUAGE());
		p.setInt(4,pg.getCOUNTER());
		p.setTimestamp(5,new Timestamp(pg.getDATE().getTime()));
		p.setString(6,pg.getPAGE_FOOTER());
		p.setString(7,pg.getPAGE_HEADER());
		p.setString(8,pg.getSIGNATURE());
		p.setString(9,pg.getSUBTITLE());
		p.setString(10,pg.getTEXT());
		p.setString(11,pg.getTITLE());
		p.setInt(12,pg.getPID());
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#addNew(org.jetnuke.entities.Pages)
	 */
	public int addNew(Pages page) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PageModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PagesModel.lastGeneratedPageID"));
		initNewPage(page, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	/**
	 * @param page
	 * @param p
	 */
	private void initNewPage(Pages pg, PreparedStatement p) throws Exception{
		p.setInt(1,pg.getACTIVE());
		p.setInt(2,pg.getCID());
		p.setString(3,pg.getCLANGUAGE());
		p.setInt(4,pg.getCOUNTER());
		p.setTimestamp(5,new Timestamp(pg.getDATE().getTime()));
		p.setString(6,pg.getPAGE_FOOTER());
		p.setString(7,pg.getPAGE_HEADER());
		p.setString(8,pg.getSIGNATURE());
		p.setString(9,pg.getSUBTITLE());
		p.setString(10,pg.getTEXT());
		p.setString(11,pg.getTITLE());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#selectAllActive(int)
	 */
	public List selectAllActive(int cid)throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.selectAllActive"));
		p.setInt(1,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#selectAllActive()
	 */
	public List selectAllActive() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.selectAllActive1"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#countActiveByCID(int)
	 */
	public int countActiveByCID(int cid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.countActiveByCID"));
		p.setInt(1,cid);
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.PagesModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PageModel.selectOrderCounterDesc"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

}
